/*
 * raccoon.c
 *
 *  Created on: 29/10/2008
 *      Author: row
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <img.h>
#include <math.h>

#define RACCOON_FILE "/usr/img/raccoon.bmp"
#define INC 1

int init(imgT ** screen, imgT ** raccoon, imgT **  forest, imgT **  buffer,char *forest_file);
void animate(imgT * screen, imgT * raccoon, imgT * forest, imgT * buffer);

int main(int argc, char ** argv)
{
	imgT * screen, * raccoon, * forest, * buffer;

	if (argc!=2)
	{
		printf("Please give the raccoon a landscape!\n");
		return 0;
	}


	if (init(&screen, &raccoon, &forest, &buffer,argv[1]) == 0)
		return 0;

	animate(screen, raccoon, forest, buffer);

	free(raccoon->image);
	free(raccoon);
	free(forest->image);
	free(forest);
	free(buffer->image);
	free(buffer);

	imgFree(screen);

	printf("Raccoon is dead!\n");

	return 0;
}

int init(imgT ** screen, imgT ** raccoon, imgT ** forest, imgT ** buffer,char *forest_file)
{

	if ((*raccoon = imgLoad(RACCOON_FILE, IMG_BMP)) == NULL)
	{
		printf("Bad image file, or not enough memory.\n");
		return 0;
	}

	if ((*forest = imgLoad(forest_file, IMG_BMP)) == NULL)
	{
		printf("Bad image file, or not enough memory.\n");
		return 0;
	}

	if ((*buffer = imgCreate((*raccoon)->w/2, (*raccoon)->h/2)) == NULL)
	{
		printf("Not enough memory.\n");
		return 0;
	}

	if ((*screen = imgScreen(getResolutionCode(*forest))) == NULL)
	{
		printf("Couldn't create screen, not enough memory.\n");
		return 0;
	}

	return 1;
}

void animate(imgT * screen, imgT * raccoon, imgT * forest, imgT * buffer)
{
	rectT pos, mask, racc, buff, frst;
	int offset = 0;
	int inc = -1;
	int i;
	int flags,aux;
	
	int size;
	
	colorT color = {0xFF, 0xFF, 0xFF};

	/*Initialize some data*/
	pos.w = raccoon->w/2;
	pos.h = raccoon->h/2;
	pos.orig.x = screen->w;
	pos.orig.y = screen->h - 20 - raccoon->h/2;

	mask.w = raccoon->w/2;
	mask.h = raccoon->h/2;
	mask.orig.x = 0;
	mask.orig.y = raccoon->h/2;

	racc.h = raccoon->h/2;
	racc.w = raccoon->w/2;
	racc.orig.x = 0;
	racc.orig.y = 0;

	buff.w = raccoon->w/2;
	buff.h = raccoon->h/2;
	buff.orig.x = 0;
	buff.orig.y = 0;

	frst.w = screen->w;
	frst.h = screen->h;
	frst.orig.x = 0;
	frst.orig.y = 0;

	imgBlt(forest, frst, screen, frst, BLT_CPY);
	imgScreenRefresh(screen);
	

	flags = fcntl(0, F_GETFL, 0);

	/*Pongo non block*/
	fcntl(0, F_SETFL, flags | O_NONBLOCK);

	while (read(0,&aux,1)==-1)
	{
		if (pos.orig.x % 20 == 0)
		{
			mask.orig.x = mask.orig.x != 0? 0 : raccoon->w/2;
			racc.orig.x = mask.orig.x;
		}

		if (pos.orig.x == -raccoon->w/2)
			pos.orig.x = screen->w;

		/*Salvar el fondo*/
		imgBlt(screen, pos, buffer, buff, BLT_CPY);
		/*Bltear la mascara*/
		imgBlt(raccoon, mask, screen, pos, BLT_AND);
		/*Bltear la imagen en si*/
		imgBlt(raccoon, racc, screen, pos, BLT_OR);
		/*Refrescar la pantalla*/
		imgScreenRefresh(screen);
		/*Tapar la imagen con el fondo salvado*/
		imgBlt(buffer, buff, screen, pos, BLT_CPY);
		pos.orig.x += inc;
		
		
	}

	/*Pongo block*/
	fcntl(0, F_SETFL, flags);

}
